package com.erp.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.common.response.ListVo;
import com.erp.common.utils.BeanUtils;
import com.erp.user.entity.UserRoles;
import com.erp.user.mapper.UserRolesMapper;
import com.erp.user.request.*;
import com.erp.user.response.UserRolesDetailInfoResp;
import com.erp.user.response.UserRolesInfoResp;
import com.erp.user.response.UserRolesSearchInfoResp;
import com.erp.user.service.UserRolesService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


/**
 * @author shensong
 * @program machining_system.v1.0.0
 * @description  服务实现类
 * @date 2022/12/21 10:55:46
 */
@Service("userRolesService")
public class UserRolesServiceImpl extends ServiceImpl<UserRolesMapper, UserRoles> implements UserRolesService {

    @Override
    public ListVo<List<UserRolesInfoResp>> index(UserRolesPageReq request) {
        LambdaQueryWrapper<UserRoles> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
            queryWrapper.eq(Objects.nonNull(request.getId()),UserRoles::getId, request.getId());
            queryWrapper.eq(Objects.nonNull(request.getUid()),UserRoles::getUid, request.getUid());
            queryWrapper.eq(Objects.nonNull(request.getRoleId()),UserRoles::getRoleId, request.getRoleId());
            queryWrapper.eq(Objects.nonNull(request.getGmtCreate()),UserRoles::getGmtCreate, request.getGmtCreate());
            queryWrapper.eq(Objects.nonNull(request.getGmtModified()),UserRoles::getGmtModified, request.getGmtModified());
        }
        queryWrapper.orderByDesc(UserRoles::getId);
        IPage<UserRoles> data = this.queryPage(request.getPageNum(), request.getPageSize(), queryWrapper);
        if (Objects.nonNull(data) && CollectionUtils.isNotEmpty(data.getRecords())) {
            List<UserRolesInfoResp> userRolesListResponse = BeanUtils.copyObjects(data.getRecords(), UserRolesInfoResp.class);
            return new ListVo<>(userRolesListResponse, data.getTotal(), data.getSize(), data.getCurrent());
        }

        return new ListVo<>();
    }

    @Override
    public Boolean store(UserRolesAddReq request) {
        UserRoles entity = BeanUtils.copyProperties(request, UserRoles.class);
        return this.save(entity);
    }

    @Override
    public Boolean update(UserRolesUpdateReq request) {
        UserRoles entity = BeanUtils.copyProperties(request, UserRoles.class);
        return this.updateById(entity);
    }

    @Override
    public Boolean destroy(UserRolesDeleteReq request) {
        return this.removeById(request.getUid());
    }

    @Override
    public UserRolesInfoResp show(UserRolesInfoReq request) {
        UserRoles entity = this.getById(request.getUid());
        return Objects.nonNull(entity) ? BeanUtils.copyProperties(entity, UserRolesInfoResp.class) : null;
    }

    @Override
    public Boolean approve(UserRolesApproveReq request) {
        UserRoles entity = BeanUtils.copyProperties(request, UserRoles.class);
        return this.updateById(entity);
    }

    @Override
    public Boolean enabled(UserRolesStatusReq request) {
        UserRoles entity = BeanUtils.copyProperties(request, UserRoles.class);
        return this.updateById(entity);
    }

    @Override
    public List<UserRolesInfoResp> option(UserRolesListReq request) {
        LambdaQueryWrapper<UserRoles> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
            queryWrapper.eq(Objects.nonNull(request.getId()), UserRoles::getId, request.getId());
            queryWrapper.eq(Objects.nonNull(request.getUid()), UserRoles::getUid, request.getUid());
            queryWrapper.eq(Objects.nonNull(request.getRoleId()), UserRoles::getRoleId, request.getRoleId());
            queryWrapper.eq(Objects.nonNull(request.getGmtCreate()), UserRoles::getGmtCreate, request.getGmtCreate());
            queryWrapper.eq(Objects.nonNull(request.getGmtModified()), UserRoles::getGmtModified, request.getGmtModified());
        }

        queryWrapper.orderByDesc(UserRoles::getId);
        List<UserRoles> data = this.list(queryWrapper);
        return CollectionUtils.isNotEmpty(data) ? BeanUtils.copyObjects(data, UserRolesInfoResp.class) : Lists.newArrayList();
    }

    @Override
    public List<UserRolesSearchInfoResp> search(UserRolesSearchReq request) {
        LambdaQueryWrapper<UserRoles> queryWrapper = Wrappers.lambdaQuery();
        if (Objects.nonNull(request)) {
                queryWrapper.eq(Objects.nonNull(request.getId()), UserRoles::getId, request.getId());
                queryWrapper.eq(Objects.nonNull(request.getUid()), UserRoles::getUid, request.getUid());
                queryWrapper.eq(Objects.nonNull(request.getRoleId()), UserRoles::getRoleId, request.getRoleId());
                queryWrapper.eq(Objects.nonNull(request.getGmtCreate()), UserRoles::getGmtCreate, request.getGmtCreate());
                queryWrapper.eq(Objects.nonNull(request.getGmtModified()), UserRoles::getGmtModified, request.getGmtModified());
        }

        queryWrapper.orderByDesc(UserRoles::getId);
        List<UserRoles> data = this.list(queryWrapper);
        return CollectionUtils.isNotEmpty(data) ? BeanUtils.copyObjects(data, UserRolesSearchInfoResp.class) : Lists.newArrayList();
    }

    @Override
    public UserRolesDetailInfoResp detail(UserRolesInfoReq request) {
        return BeanUtils.copyProperties(this.show(request), UserRolesDetailInfoResp.class);
    }

    private IPage<UserRoles> queryPage(Long pageNum, Long pageSize, Wrapper<UserRoles> queryWrapper) {
        IPage<UserRoles> selectPage = new Page<>();
        selectPage.setCurrent(pageNum);
        selectPage.setSize(pageSize);

        IPage<UserRoles> page = this.baseMapper.selectPage(selectPage, queryWrapper);
        return page;
    }
}
